/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Buslog;

import ActiveRecord.Newdb.Db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author degrave
 */
public class Event {

    protected int id;
    protected int ownerCalendarId;
    protected String date;
    protected String time;
    protected String text;

    public Event(int newid, String newdate, String newtime, String newtext, int ownerCalendarId) {
        id = newid;
        date = newdate;
        time = newtime;
        text = newtext;
        this.ownerCalendarId = ownerCalendarId;
    }

    public Event() {
        id = 0;
        date = null;
        time = null;
        text = null;
        this.ownerCalendarId = 0;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getownerCalendar() {
        return ownerCalendarId;
    }

    public void setownerCalendarIdId(int id) {
        this.ownerCalendarId = id;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void pushToDB() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            s.executeUpdate("insert into EVENTS (id, date, time, text, ownerid)"
                    + " values (" + id + ",'" + date + "','" + time + "','" + text + "'," + ownerCalendarId + ")");
            System.out.println("inserted into event table");
        } catch (SQLException ex) {
            System.out.println("Cant insert into event table");
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void removeFromDB(int owner) {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            s.executeUpdate("delete from EVENTS where ownerid = " + owner);
            System.out.println("Removed from event table");
        } catch (SQLException ex) {
            System.out.println("cant remove from event table");
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void removeFromDB(String date, String time) {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            s.executeUpdate("delete from EVENTS where DATE = '" + date + "' AND TIME = '" + time + "'");
            System.out.println("Removed from event table");
        } catch (SQLException ex) {
            System.out.println("cant remove from event table");
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static int getMaxId() {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("select MAX(id) from EVENTS");
            if (rs.next()) {
                System.out.println("Selected maxid from event table");
                return rs.getInt(1);
            } else {
                return 0;
            }
        } catch (SQLException ex) {
            System.out.println("Cant select maxid from event table");
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
    }

    public static ResultSet findEvent(String dateOrTime, int key) {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs;
            if (key == 1) {
                rs = s.executeQuery("select * from EVENTS where DATE = '" + dateOrTime + "'");
                System.out.println("Finde event from event table");
                return rs;
            }
            if (key == 2) {
                rs = s.executeQuery("select * from EVENTS where TIME = '" + dateOrTime + "'");
                System.out.println("Finde event from event table");
                return rs;
            }
            //rs.next();
            //Event event = new Event(rs.getInt(1), rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5));       
        } catch (SQLException ex) {
            System.out.println("Cant finde event from event table");
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public static Event findEvent(int eventId) {
        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("select * from EVENTS where ID = " + eventId + "");
            if (rs.next()) {
                Event event = new Event(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getInt(5));
                System.out.println("Finde event from event table");
                return event;
            }
        } catch (SQLException ex) {
            System.out.println("Cant find event from event table");
            Logger.getLogger(Event.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (new Event(0, "", "", "", 0));

    }

    public static ResultSet findAllEvents(int owner) {

        try {
            Connection c = Db.getInstance().getConnection();
            java.sql.Statement s = c.createStatement();
            ResultSet rs = s.executeQuery("SELECT * FROM EVENTS where ownerid = " + owner + "");
            System.out.println("Finde all events from event table");
            return rs;
        } catch (SQLException ex) {
            System.out.println("Cant finde all events from event table");
            Logger.getLogger(ActiveRecord.UserActiveRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
